package com.github.vertical_blank.sqlformatter.Demo;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 提取sql & params
 */

public class RegexDemo {

    public static void main(String[] args) {
        String input = "sql-->name[easmt.tenant.getPlatformInfoByName],param[127.0.0.1, 30028, 5],use[3669],sql[select distinct tenant_platform.platformname, platform.enname platformenname, tenant_platform.logourl platformlogourl, tenant.name tenantname, tenant.logourl tenantlogourl, tenant_platform.domain, tenant.id tenantid, platform.id platformid, tenant_platform.fullname, tenant.copyright, tenant.address, tenant.schoolname, tenant.mobile, tenant.collegename, tenant.postcode, tenant.loginlogo, tenant.status tenantstatus, tenant_platform.status, platform.authzlevel from tenant_platform inner join platform on platform.id = tenant_platform.platformid inner join tenant on tenant.id = tenant_platform.tenantid and tenant.isdelete = 0 where tenant_platform.domain = ? order by platform.id limit 1,5 ;],login[0],guid[]^";

        // 正则表达式匹配param字段
        Pattern paramPattern = Pattern.compile("param\\[(.*?)\\]");
        Matcher paramMatcher = paramPattern.matcher(input);

        // 正则表达式匹配sql字段
        Pattern sqlPattern = Pattern.compile("sql\\[(.*?)\\]");
        Matcher sqlMatcher = sqlPattern.matcher(input);

        if (paramMatcher.find() && sqlMatcher.find()) {
            String param = paramMatcher.group(1); // 捕获param的内容
            String sql = sqlMatcher.group(1);     // 捕获sql的内容

            System.out.println("Param: " + param);
            System.out.println("SQL: " + sql);
        } else {
            System.out.println("No match found.");
        }
    }
}

